<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<!--旧版-->
<!--xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"-->
<!--新版-->
<!--http://www.thymeleaf.org/extras/spring-security-->

<head>
    <meta charset="UTF-8">
    <title>Index</title>
</head>
<body>
首页

<div th:fragment="head">
    <div sec:authorize="!isAuthenticated()">
        <a th:href="@{/toLogin}">登录</a>
    </div>

    <div sec:authorize="isAuthenticated()">
        <span>用户名: <span sec:authentication="name"></span></span>
        <a th:href="@{/logout}">注销</a>
    </div>
</div>

<!--根据权限控制是否显示-->
<div>
    <div sec:authorize="hasRole('vip')">
        <a th:href="@{/level/1}">Level 1</a>
    </div>
    <div sec:authorize="hasRole('vip2')">
        <a th:href="@{/level/2}">Level 2</a>
    </div>
    <div sec:authorize="hasRole('vip3')">
        <a th:href="@{/level/3}">Level 3</a>
    </div>
</div>
</body>
</html>